package my.jieh.tips.swing;

/**
 * <p>Title: Core Java Technologies Tech Tips</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Blueimp</p>
 * @author jieh
 * @version 1.0
 */
import java.awt.*;
import javax.swing.*;
import java.awt.font.*;
import java.text.*;

public class Multi4 extends JFrame {
	String input =
		"EDWARD by the grace of God, King of " +
		"England, Lord of Ireland, and Duke of " +
		"Guyan, to all Archbishops, Bishops, etc. " +
		"We have seen the Great Charter of the " +
		"Lord HENRY, sometimes King of England, " +
		"our father, of the Liberties of England, " +
		"in these words: ";
	AttributedString attributedString;

	public Multi4() {
		super("Manual Painting");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Font font = (Font)UIManager.get("Label.font");
		attributedString = new AttributedString(input);
		attributedString.addAttribute(TextAttribute.FONT, font);
		Color color =	(Color)UIManager.get("Label.foreground");
		attributedString.addAttribute(TextAttribute.FOREGROUND, color);
		setSize(300, 200);
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;

		// Watch the margins
		Insets insets = getInsets();
		int width = getSize().width -	insets.right - insets.left;
		// Set the starting position to draw
		int x = insets.left;
		int y = insets.top;

		// Get iterator for string
		AttributedCharacterIterator characterIterator =
			attributedString.getIterator();
		// Get font context from graphics
		FontRenderContext fontRenderContext =
			g2d.getFontRenderContext();
		// Create measurer
		LineBreakMeasurer measurer =
			new LineBreakMeasurer(characterIterator,
				fontRenderContext);
		while (measurer.getPosition() <
				characterIterator.getEndIndex()) {
			// Get line
			TextLayout textLayout =
				measurer.nextLayout(width);
			// Move down to baseline
			y += textLayout.getAscent();
			// Draw line
			textLayout.draw(g2d, x, y);
			// Move down to top of next line
			y += textLayout.getDescent() +
				textLayout.getLeading();
		}
	}

	public static void main(String args[]) {
		JFrame frame = new Multi4();
		frame.show();
	}
}